'use client';

import { useRouter } from 'next/navigation';
import { memo, useEffect } from 'react';

import { messageService } from '@/services/message';
import { sessionService } from '@/services/session';
// import { useSessionStore } from '@/store/session';

import { configService } from '@/services/config';
import config from '@/config/LobeChat-config-v3.json'
import { ConfigFile } from '@/types/exportConfig';

const checkHasConversation = async () => {
  const hasMessages = await messageService.hasMessages();
  const hasAgents = await sessionService.hasSessions();
  return hasMessages || hasAgents;
};

const Redirect = memo(() => {
  const router = useRouter();
  // const [switchSession] = useSessionStore((s) => [s.switchSession]);

  useEffect(() => {
    checkHasConversation().then(async (hasData) => {
      if (!hasData) {
        console.log(config)
        await configService.importConfigState(config as ConfigFile)
        window.location.reload()
      }
      router.replace('/welcome')
      // if (hasData) {
      //   router.replace('/chat');

      //   switchSession();
      // } else {
      //   router.replace('/welcome');
      // }
    });
  }, []);

  return null;
});

export default Redirect;
